import 'dart:io';

import 'package:cashbook/utils/CacheManager.dart';
import 'package:cashbook/utils/SizeUtil.dart';
import 'package:cashbook/utils/db_util/dbmanager.dart';
import 'package:cashbook/utils/manager.dart';
import 'package:cashbook/utils/screen_util.dart';
import 'package:cashbook/utils/toast.dart';
import 'package:flutter/material.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:path_provider/path_provider.dart';

class MePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return MePageState();
  }
}

class MePageState extends State<MePage> {
  var dataSize = '';
  var cacheSize = '';

  @override
  Widget build(BuildContext context) {
    getDBSize();
    getCacheSize();
    return Center(
      child: Container(
        margin:
            EdgeInsets.only(top: 10 + ScreenUtil.getSysStatsHeight(context)),
        child: Column(
          children: <Widget>[
            ClipOval(
              child: Container(
                padding: EdgeInsets.all(2),
                color: Colors.grey,
                child: ClipOval(
                  child: Image.asset(
                    'images/defouthead.jpg',
                    width: 80,
                    height: 80,
                  ),
                ),
              ),
            ),
            Container(
              margin: EdgeInsets.only(top: 10),
              child: Text(
                '昵称：${Manager.instance.loginUser.nickName == null ? 'null' : Manager.instance.loginUser.nickName}',
                style: TextStyle(fontWeight: FontWeight.w600),
              ),
            ),
            itemView('设置', Icons.arrow_forward_ios, top: 30),
            itemView('缓存', Icons.arrow_forward_ios, secondText: cacheSize,
                callback: () {
              clearCache();
            }),
            itemView('数据', Icons.arrow_forward_ios, secondText: dataSize,
                callback: () {
              //Toast.toast(context, '数据');
            }),
          ],
        ),
      ),
    );
  }

  itemView(String text, IconData iconDate,
      {double top = 1, Function callback, String secondText}) {
    return Container(
      height: 50,
      padding: EdgeInsets.only(left: 10, right: 10),
      color: Colors.white,
      margin: EdgeInsets.only(top: top),
      child: GestureDetector(
        onTap: callback,
        child: Row(
          children: <Widget>[
            Expanded(
              child: Text(text),
            ),
            secondText == null ? Container() : Text(secondText),
            Icon(
              iconDate,
              color: Colors.grey,
            )
          ],
        ),
      ),
    );
  }

  void getDBSize() async {
    String path = DBManager.dbPath;
    File file = await File(path);
    //文件大小
    int fileLength = await file.length();
    String size = SizeUtil.getNetFileSizeDescription(fileLength);
    setState(() {
      dataSize = size;
    });
  }

  void clearCache() async {
    bool flag = await CacheManager.clearCache();
    if (flag) {
      Toast.toast(
        context,
        '清除成功',
      );
    } else {
      Toast.toast(
        context,
        '清除失败',
      );
    }
  }

  void getCacheSize() async{
    String path = await CacheManager.getCachePath();
    File file = await File(path);
    //文件大小
    int fileLength = await file.length();
    String size = SizeUtil.getNetFileSizeDescription(fileLength);
    setState(() {
      cacheSize = size;
    });
  }
}
